/****************************************************************************
Copyright (c) 2023 - 2024 Qualcomm Technologies International, Ltd.


FILE NAME
    upgrade_post_erase.c

DESCRIPTION
    Upgrade post erase processing state machine.
    Partition data are erased from the SQIF.

NOTES

*/

#define DEBUG_LOG_MODULE_NAME upgrade
#include <logging.h>
#include <string.h>
#include <byte_utils.h>
#include <panic.h>
#include <ps.h>
#include <system_clock.h>

#include <imageupgrade.h>
#include <stdlib.h>
#include "upgrade_ctx.h"
#include "upgrade_private.h"
#include "upgrade_psstore.h"
#include "upgrade_fw_if.h"
#include "upgrade_post_erase.h"
#include "upgrade.h"
#include "onchip_coredump.h"


/****************************************************************************
NAME
    UpgradePostEraseStart

DESCRIPTION
    Start the Post DFU Erase.

*/

bool UpgradePostEraseStart(void)
{
    if(OnchipCoredumpIsPresent(ONCHIP_COREDUMP_MAXIDUMP))
    {
        DEBUG_LOG_VERBOSE("UpgradePostEraseStart, on chip core dump available");
        return FALSE;
    }
    else
    
    {
        DEBUG_LOG_INFO("UpgradePostEraseStart, Erasing only image header");
        UpgradeCtxGet()->erase_state = UPGRADE_POST_DFU_ERASE_STATE_HEADER_ERASING;
        UpgradeFWIFEraseSection(UPGRADE_FW_IF_FILTER_IMAGE_HEADER);
        return FALSE;
    }
}


/****************************************************************************
NAME
    UpgradePostErase

DESCRIPTION
    Trigger the Post DFU Erase process.
*/
void UpgradePostErase(void)
{
    UpgradePostEraseStart();
}

/****************************************************************************
NAME
    UpgradePostEraseSetState

DESCRIPTION
    Set the upgrade in post DFU erase state.

*/
void UpgradePostEraseSetState(bool erase_in_progress)
{
    if(erase_in_progress)
    {
        UpgradeCtxGetPSKeys()->upgrade_property |= UPGRADE_PROPERTY_UPGRADE_POST_DFU_ERASE_IN_PROGRESS;
        UpgradeCtxGet()->erase_state = UPGRADE_POST_DFU_ERASE_STATE_SECTIONS_ERASING;
    }
    else
    {
        UpgradeCtxGetPSKeys()->upgrade_property &= ~UPGRADE_PROPERTY_UPGRADE_POST_DFU_ERASE_IN_PROGRESS;
        UpgradeCtxGet()->erase_state = UPGRADE_POST_DFU_ERASE_STATE_NONE_ERASED;
    }
    UpgradeSavePSKeys();
}

/****************************************************************************
NAME
    UpgradePostEraseInProgress

DESCRIPTION
    Indicates whether the upgrade is in post DFU erase state.

*/
bool UpgradePostEraseInProgress(void)
{
    if(UpgradeCtxGetPSKeys()->upgrade_property & UPGRADE_PROPERTY_UPGRADE_POST_DFU_ERASE_IN_PROGRESS)
    {
        DEBUG_LOG_VERBOSE("UpgradePostEraseInProgress In progress");
        return TRUE;
    }
    else
    {
        DEBUG_LOG_VERBOSE("UpgradePostEraseInProgress Not In progress");
        return FALSE;
    }
}


/****************************************************************************
NAME
    UpgradePostEraseHandleMessages

DESCRIPTION
    Message Handler for Post DFU Erase Messages.
*/


void UpgradePostEraseHandleMessages(MessageId id, Message message)
{
    switch(UpgradeCtxGet()->erase_state)
        {
            case UPGRADE_POST_DFU_ERASE_STATE_SECTIONS_ERASING:
                DEBUG_LOG_INFO("UpgradePostEraseHandleMessages Erasing Image Header");
                UpgradeFWIFEraseSection(UPGRADE_FW_IF_FILTER_IMAGE_HEADER);
                /*We are restarting the post DFU erase process if the device resets.
                If we were erasing the image-header before the reset then after the reset,
                the erase of BANK_SECTIONS will fail as the partial image-header table is
                erased but still we need to erase the header so, we will continue to next 
                step with out checking the erase status.*/
                UpgradeCtxGet()->erase_state = UPGRADE_POST_DFU_ERASE_STATE_HEADER_ERASING;
            break;
            case UPGRADE_POST_DFU_ERASE_STATE_HEADER_ERASING:
                DEBUG_LOG_INFO("UpgradePostEraseHandleMessages Erase Completed");
                UpgradePostEraseSetState(FALSE);
                MessageSend(UpgradeGetAppTask(), UPGRADE_POST_ERASE_COMPLETE_IND, NULL);
            break;
            default:
            break;
        }
}

